#include "dialogloop.h"
#include "ui_dialogloop.h"
#include "appsettings.h"
#include "appconst.h"
#include "TStringUtil.h"
#include "excelfile.h"
#include "appconst.h"
#include <QFileDialog>
#include "userdatarole.h"



DialogLoop::DialogLoop(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogLoop)
{
    ui->setupUi(this);
}

DialogLoop::~DialogLoop()
{
    delete ui;
}

void DialogLoop::on_pushButtonAdd_clicked()
{
    QTreeWidgetItem* item = ui->treeWidget->currentItem();
    if (!item)
    {
        return;
    }

    if (item->parent() == NULL)
    {
        QString num = item->data(0, kNumRole).toString();
        size_t index = m_freqList.indexOf(num.toStdString());
        if (index != -1)
        {
            addPrograms(num.toStdString());
        }
    }
    else
    {
        QString num = item->data(0, kNumRole).toString();
        size_t index = m_programList.indexOf(num.toStdString());
        if (index != -1)
        {
            const tv::Program& program = m_programList[index];
            m_loopList.push_back(program);
            appendProgram(program);
        }
    }

}

void DialogLoop::on_pushButtonAddAll_clicked()
{
    for (size_t i = 0; i < m_programList.size(); ++ i)
    {
        const tv::Program& program = m_programList[i];
        m_loopList.push_back(program);
        appendProgram(program);
    }
}

void DialogLoop::on_pushButtonDelete_clicked()
{
    int row = ui->tableWidget->currentRow();
    if (row < 0 || row >= ui->tableWidget->rowCount())
    {
        return;
    }

    m_loopList.remove(row);
    ui->tableWidget->removeRow(row);
}

void DialogLoop::on_pushButtonDeleteAll_clicked()
{
    m_loopList.clear();

    refreshTable();
}

void DialogLoop::on_pushButtonImport_clicked()
{
    QFileDialog fileDialog(this);
    fileDialog.setFileMode(QFileDialog::ExistingFile);
    if (fileDialog.exec() != QDialog::Accepted)
    {
        return;
    }

    QStringList fileList = fileDialog.selectedFiles();
    if (fileList.empty())
    {
        return;
    }

    std::string filepath = fileList[0].toStdString();
    ExcelFile::load(filepath, m_loopList);

    refreshTable();

}

void DialogLoop::on_pushButtonExport_clicked()
{
	QString dir = AppConst::getQAppDir();

    QString filename = QFileDialog::getSaveFileName(this, tr("Save File"), dir,
        tr("Excel file (*.xlsx)"));

    std::string filepath = filename.toStdString();
    ExcelFile::save(m_loopList, filepath);
}

void DialogLoop::setProgramList(const tv::FrequenceList& freqList, const tv::ProgramList& programList)
{
    m_freqList = freqList;
    m_programList = programList;

    refreshProgramTree();
}

void DialogLoop::setLoopList(const tv::ProgramList& programList)
{
    m_loopList = programList;

    refreshTable();
}

tv::ProgramList& DialogLoop::getLoopList()
{
    return m_loopList;
}


void DialogLoop::setProgram(int row, const tv::Program& program)
{
    int col = 0;

    {
        QTableWidgetItem* item = new QTableWidgetItem();
        item->setText(program.num.c_str());

        Qt::ItemFlags flags = item->flags();
        flags &= ~Qt::ItemIsEditable;
        item->setFlags(flags);

        item->setData(kNumRole, program.num.c_str());

        ui->tableWidget->setItem(row, col ++, item);
    }

    {
        QTableWidgetItem* item = new QTableWidgetItem();
        item->setText(program.name.c_str());
        Qt::ItemFlags flags = item->flags();
        flags &= ~Qt::ItemIsEditable;
        item->setFlags(flags);
        ui->tableWidget->setItem(row, col ++, item);
    }


    {
        QTableWidgetItem* item = new QTableWidgetItem();
        item->setText(program.freqNum.c_str());
        Qt::ItemFlags flags = item->flags();
        flags &= ~Qt::ItemIsEditable;
        item->setFlags(flags);
        ui->tableWidget->setItem(row, col ++, item);
    }

    {
        QTableWidgetItem* item = new QTableWidgetItem();
        item->setText(program.freqName.c_str());
        Qt::ItemFlags flags = item->flags();
        flags &= ~Qt::ItemIsEditable;
        item->setFlags(flags);
        ui->tableWidget->setItem(row, col ++, item);
    }

    {
        QTableWidgetItem* item = new QTableWidgetItem();
        item->setText(program.url.c_str());
        Qt::ItemFlags flags = item->flags();
        flags &= ~Qt::ItemIsEditable;
        item->setFlags(flags);
        ui->tableWidget->setItem(row, col ++, item);
    }

    {
        QTableWidgetItem* item = new QTableWidgetItem();
        item->setText(program.memo.c_str());
        Qt::ItemFlags flags = item->flags();
        flags &= ~Qt::ItemIsEditable;
        item->setFlags(flags);
        ui->tableWidget->setItem(row, col ++, item);
    }
}

void DialogLoop::appendProgram(const tv::Program& program)
{
    int rowCount = ui->tableWidget->rowCount();
    ui->tableWidget->insertRow(rowCount);
    setProgram(rowCount, program);
}

void DialogLoop::refreshTable()
{
    ui->tableWidget->setRowCount(0);
    ui->tableWidget->clearContents();

    for (size_t i = 0; i < m_loopList.size(); ++i)
    {
        const tv::Program& program = m_loopList[i];

        appendProgram(program);
    }
}


void DialogLoop::refreshProgramTree()
{
    ui->treeWidget->clear();

    for (size_t i = 0; i < m_freqList.size(); ++ i)
    {
        const tv::Frequence& freq = m_freqList[i];
        QTreeWidgetItem* item = new QTreeWidgetItem();

        item->setData(0, kNumRole, freq.num.c_str());

        item->setText(0, freq.name.c_str());
        item->setIcon(0, QIcon(":/images/freq"));

        ui->treeWidget->insertTopLevelItem(i, item);
    }

    for (size_t i = 0; i < m_programList.size(); ++ i)
    {
        const tv::Program& program = m_programList[i];
        QTreeWidgetItem* topItem = findFreqItem(program.freqNum);
        if (!topItem)
        {
            continue;
        }

        QTreeWidgetItem* item = new QTreeWidgetItem();

        item->setData(0, kNumRole, program.num.c_str());
        item->setData(0, kNameRole, program.name.c_str());
        item->setData(0, kUrlRole, program.url.c_str());

        item->setText(0, program.name.c_str());
        item->setIcon(0, QIcon(":/images/program"));

        topItem->addChild(item);
    }


    ui->treeWidget->expandAll();
}

QTreeWidgetItem* DialogLoop::findFreqItem(const std::string& num)
{
    QTreeWidgetItem* item = NULL;
    int count = ui->treeWidget->topLevelItemCount();
    for (int i = 0; i < count; i ++)
    {
        QTreeWidgetItem* topItem = ui->treeWidget->topLevelItem(i);
        QString numData = topItem->data(0, kNumRole).toString();
        if (numData == num.c_str())
        {
            item = topItem;
            break;
        }
    }
    return item;
}

void DialogLoop::addPrograms(const std::string& freqNum)
{
    for (size_t i = 0; i < m_programList.size(); ++ i)
    {
        const tv::Program& program = m_programList[i];
        if (program.freqNum == freqNum)
        {
            m_loopList.push_back(program);
            appendProgram(program);
        }
    }
}
